package com.net.xpay.common.service.impl;

import com.net.xpay.common.domain.stat.CompanyBillDailyStat;
import com.net.xpay.core.manager.ChargeRecordManager;
import com.net.xpay.core.manager.PayTransBatchManager;
import com.net.xpay.core.manager.UserWithdrawRecordManager;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import com.net.xpay.core.model.CompanyBillDailyStatModel;
import com.net.xpay.core.model.SafePayTransBankDailyStatModel;
import com.net.common.util.LocalDateUtil;
import com.net.xpay.common.service.CompanyBillDailyStatInnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 2020/9/22 17:00

 */
@Slf4j
@Service
public class CompanyBillDailyStatInnerServiceImpl implements CompanyBillDailyStatInnerService {

    @Autowired
    private PayTransBatchManager payTransBatchManager;

    @Autowired
    private ChargeRecordManager chargeRecordManager;

    @Autowired
    private SafePayBatchManager safePayBatchManager;

    @Autowired
    private UserWithdrawRecordManager userWithdrawRecordManager;

    /**
     * 统计日账单
     * @param day 日期
     * @return 日账单
     */
    @Override
    public CompanyBillDailyStat statDailyCompanyBill(LocalDate day) {
        // 根据当前时间获取改天的起始时间和结束时间
        LocalDateTime startTime = LocalDateUtil.getDayStart(day);
        LocalDateTime endTime = LocalDateUtil.getDayStart(day.plusDays(1L));

        CompanyBillDailyStat billDailyStat = CompanyBillDailyStat.getInstance(day);

        //转账数据
        CompanyBillDailyStatModel statModel = payTransBatchManager.statDailyCompanyBill(startTime, endTime);
        billDailyStat.setSuccessServicePrice(statModel.getSuccessServicePrice());
        billDailyStat.setRealSuccessServicePrice(statModel.getRealSuccessServicePrice());
        billDailyStat.setTotalPartnerBenefit(statModel.getTotalPartnerBenefit());
        billDailyStat.setTotalFatherPartnerBenefit(statModel.getTotalFatherPartnerBenefit());
        billDailyStat.setTotalPlatformBenefit(statModel.getTotalPlatformBenefit());
        billDailyStat.setSuccessAmount(statModel.getSuccessAmount());
        billDailyStat.setSuccessCount(statModel.getSuccessCount());

        statModel = safePayBatchManager.statDailyCompanyBill(startTime, endTime);
        billDailyStat.setSafePaySuccessServicePrice(statModel.getSafePaySuccessServicePrice());
        billDailyStat.setSafePayTotalPartnerBenefit(statModel.getSafePayTotalPartnerBenefit());
        billDailyStat.setSafePayTotalFatherPartnerBenefit(statModel.getSafePayTotalFatherPartnerBenefit());
        billDailyStat.setSafePayTotalPlatformBenefit(statModel.getSafePayTotalPlatformBenefit());
        billDailyStat.setSafePaySuccessAmount(statModel.getSafePaySuccessAmount());
        billDailyStat.setSafePaySuccessCount(statModel.getSafePaySuccessCount());

        //安全发到卡转账金额 复用了下别的地方的统计代码 结束时间需要单独处理下
        SafePayTransBankDailyStatModel safePayTransBankDailyStatModel = safePayBatchManager.statSafePayTransBankAmount(startTime, LocalDateUtil.getDayEnd(day));
        billDailyStat.setSafePayTransSuccessBankAmount(safePayTransBankDailyStatModel.getSafePayTransSuccessBankAmount());

        //充值数据
        long rechargeAmount = chargeRecordManager.statChargeAmount(startTime, endTime, 1);

        long reverseRechargeAmount = chargeRecordManager.statChargeAmount(startTime, endTime, 0);

        billDailyStat.setRechargeAmount(rechargeAmount);
        billDailyStat.setReverseRechargeAmount(reverseRechargeAmount);

        //商家提现数据
        long transferMoney = userWithdrawRecordManager.statTransferMoney(startTime, endTime);
        billDailyStat.setUserWithdrawAmount(transferMoney);

        return billDailyStat;
    }
}
